package ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.dialogs;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch
 *
 */

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import ch.unisi.inf.pfii.teamwhite.risk.core.Game;
import ch.unisi.inf.pfii.teamwhite.risk.core.player.Human;
import ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.GameFrame;

public class ChooseBuildingDialog extends JDialog{

	private Game game;
	
	private GameFrame gameFrame;
	private Container contentPane;
	private JPanel infoPanel;
	private JLabel infoLabel;
	private JLabel imageLabel;
	private ImageIcon image;
	
	private String building;
	private String infos;
	private Boolean isCapital;
	
	public ChooseBuildingDialog(Game game, GameFrame gameFrame) {
		super(gameFrame, "Choose Building", true);
		
		this.game = game;
		
		this.gameFrame = gameFrame;
		
		building = null;
		infos = "Choose a building";
		isCapital = false;
		
		contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		contentPane.add(createChoose(), BorderLayout.WEST);
		contentPane.add(building(), BorderLayout.CENTER);
		contentPane.add(createOk(), BorderLayout.SOUTH);

		setSize(300, 300);		
	    setLocationRelativeTo(null);
	    setResizable(false);
	    setVisible(true);
	}
	
//	This method creates the panel which contains the choose of the buildings
	private Component createChoose() {
		final JPanel choosePanel = new JPanel();
		choosePanel.setLayout(new GridLayout(4, 1));
		choosePanel.setBorder(BorderFactory.createTitledBorder("Choose a Building"));
		
		ButtonGroup chooseGroup = new ButtonGroup();
	    JRadioButton def = new JRadioButton("Defensive Shield");
	      chooseGroup.add(def);
	      choosePanel.add(def);
	    JRadioButton sla = new JRadioButton("Slavery Camp");
	      chooseGroup.add(sla);
	      choosePanel.add(sla);
	    JRadioButton tax = new JRadioButton("Tax Center");
	      chooseGroup.add(tax);
	      choosePanel.add(tax);
	     
	    def.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				ChooseBuildingDialog.this.building = "Defensive Shield";
				image = new ImageIcon(getClass().getResource("building/defensive.png"));
				imageLabel.setIcon(image);
				infos = "<HTML> <head> </head> <body> The Defensive Shield increments the defense rate. </body> </HTML>";
				infoLabel.setText(infos);
			}
		});
	    sla.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				ChooseBuildingDialog.this.building = "Slavery Camp";
				image = new ImageIcon(getClass().getResource("building/slavery.png"));
				imageLabel.setIcon(image);
				infos = "<HTML> <head> </head> <body> The Slavery Camp creates a new ship each turn, but reset the incomes. </body> </HTML>";
				infoLabel.setText(infos);
			}
		});
	    tax.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				ChooseBuildingDialog.this.building = "Tax Center";
				image = new ImageIcon(getClass().getResource("building/tax.png"));
				imageLabel.setIcon(image);
				infos = "<HTML> <head> </head> <body> The Tax Center increments the incomes, dependig on the neighbours planets. </body> </HTML>";
				infoLabel.setText(infos);
			}
		});
	    
	    if (gameFrame.getSelectedPlanet().isCapital()) {
	    	isCapital = true;
	    	JRadioButton eld = new JRadioButton("Eldest Council");
		      chooseGroup.add(eld);
		      choosePanel.add(eld);
		     
		      eld.addActionListener(new ActionListener() {
				public void actionPerformed(final ActionEvent ev) {
					ChooseBuildingDialog.this.building = "Eldest Council";
					image = new ImageIcon(getClass().getResource("building/council.png"));
					imageLabel.setIcon(image);
					infos = "<HTML> <head> </head> <body> The Eldest Council increments the defense rate the incomes and creates a ship each turn. </body> </HTML>";
					infoLabel.setText(infos);
				}
			});
	    }
	    
	    return choosePanel;
	}
	
//	This method creates the panel which contains the infos of the buildings
	private Component building() {
		infoPanel = new JPanel();
		infoPanel.setLayout(new GridLayout(2, 1));
		
		imageLabel = new JLabel();
        imageLabel.setBorder(BorderFactory.createTitledBorder("Building Infos"));
        imageLabel.setIcon(image);
        infoPanel.add(imageLabel);
        
        infoLabel = new JLabel();
        infoLabel.setBorder(BorderFactory.createTitledBorder("Building Infos"));
        infoLabel.setText(infos);
        infoPanel.add(infoLabel);
        
        return infoPanel;
	}
	
//	This method creates the button which accept the choice
	private Component createOk() {
		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				try{
					((Human)ChooseBuildingDialog.this.game.getCurrentPlayer()).build(building);
					ChooseBuildingDialog.this.building = null;
				}catch(Exception ex){
				}
			}
		});
		return ok;
	}
}
